package com.fyzk.activity.lottery.shuzicai;

import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fyzk.activity.down_numhistory.NumHistoryPrizeView;
import com.fyzk.activity.lottery.shuzicai.TouZhuHelper.BallClickListener;
import com.fyzk.activity.lottery.shuzicai.TouZhuHelper.BallItemClickLitener;
import com.fyzk.activity.myview.BallPopFrameLayout;
import com.fyzk.activity.myview.BallScrollView;
import com.fyzk.activity.myview.DrawBallFrameLayout;
import com.fyzk.activity.myview.WeiZhi;
import com.fyzk.activity.trend.QXCTrendActivity;
import com.fyzk.bean.PrizeHistoryItem;
import com.tyjc.lottery.R;
import com.fyzk.random.RandomFactory;
import com.fyzk.utils.HelpUrlConstants;
import com.fyzk.utils.IntentParamUtils;
import com.fyzk.utils.LotteryIDUtils;
import com.fyzk.view.TopTitleView;

import java.util.ArrayList;
import java.util.List;


public class QXCActivity extends LotteryBaseActivity implements BallClickListener,
        BallItemClickLitener {

    private BallPopFrameLayout moveQiu;

    private TopTitleView topView;

    private DrawBallFrameLayout ball_linear;
    private BallScrollView ball_scroll;

    SelectBallView ballView_firstLine;
    SelectBallView ballView_twoline;
    SelectBallView ballView_threeline;
    SelectBallView ballView_fourline;
    SelectBallView ballView_fiveline;
    SelectBallView ballView_sixline;
    SelectBallView ballView_thevenline;

    List<BallInfo> balls_firstLine = new ArrayList<BallInfo>();
    List<BallInfo> balls_twoLine = new ArrayList<BallInfo>();
    ;
    List<BallInfo> balls_threeLine = new ArrayList<BallInfo>();
    ;
    List<BallInfo> balls_fourLine = new ArrayList<BallInfo>();
    ;
    List<BallInfo> balls_fiveLine = new ArrayList<BallInfo>();
    List<BallInfo> balls_sixLine = new ArrayList<BallInfo>();
    List<BallInfo> balls_thevenLine = new ArrayList<BallInfo>();

    private TextView bottom_zhushu;

    private TextView bottom_money;
    private  LinearLayout shake_random_ll;

    //新加
    private LinearLayout ll_history;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dj_activity_qxc);
        initBallView();
//        ballClick();


    }

    @Override
    public TopTitleView getTopView() {
        return topView;
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){
            case R.id.shake_random_ll:
                randomBallNum();
                break;
        }
    }

    private void initBallView() {
        TouZhuHelper.getInstance().setBallClickListener(this);
        TouZhuHelper.getInstance().setBallItemClickLitener(this);

        topView = (TopTitleView) findViewById(R.id.topTitle);
        topView.setCenterText(R.string.qxc);
        topView.setRightVisible(View.VISIBLE);
        topView.setRightImg(R.drawable.downmenu_icon);
        topView.setOnTitleClickListener(this);
        shake_random_ll = (LinearLayout)findViewById(R.id.shake_random_ll);
        shake_random_ll.setOnClickListener(this);

        moveQiu = (BallPopFrameLayout) findViewById(R.id.pl5_fl_moveqiu);
        ball_linear = (DrawBallFrameLayout) findViewById(R.id.pl5_drawball_framelayout);
        ball_scroll = (BallScrollView) findViewById(R.id.pl5_ballscroll);
        ball_linear.setScr(ball_scroll);
        ball_linear.setFl_moveqiu(moveQiu);
        ball_linear.setUpOrDownListener(this);
        ballView_firstLine = (SelectBallView) findViewById(R.id.qxc_firstLine);
        ballView_twoline = (SelectBallView) findViewById(R.id.qxc_twoLine);
        ballView_threeline = (SelectBallView) findViewById(R.id.qxc_thirdLine);
        ballView_fourline = (SelectBallView) findViewById(R.id.qxc_fourLine);
        ballView_fiveline = (SelectBallView) findViewById(R.id.qxc_fiveLine);
        ballView_sixline = (SelectBallView) findViewById(R.id.qxc_sixLine);
        ballView_thevenline = (SelectBallView) findViewById(R.id.qxc_thevenLine);
        bottom_zhushu = (TextView) findViewById(R.id.pl5_bottom_tv_zhushu);
        bottom_money = (TextView) findViewById(R.id.pl5_bottom_tv_money);

        //新加
        ll_history = (LinearLayout) findViewById(R.id.qxc_ball_history);

        initArrayInfos();

        initBottomView();
    }

    @Override
    protected void initArrayInfos() {
        if (arr_infos.size() == 7) {
            balls_firstLine = arr_infos.get(0);
            balls_twoLine = arr_infos.get(1);
            balls_threeLine = arr_infos.get(2);
            balls_fourLine = arr_infos.get(3);
            balls_fiveLine = arr_infos.get(4);
            balls_sixLine = arr_infos.get(5);
            balls_thevenLine = arr_infos.get(6);
        } else {
            arr_infos.clear();
            initBallList(balls_firstLine, 10, 0, true);
            initBallList(balls_twoLine, 10, 0, true);
            initBallList(balls_threeLine, 10, 0, true);
            initBallList(balls_fourLine, 10, 0, true);
            initBallList(balls_fiveLine, 10, 0, true);
            initBallList(balls_sixLine, 10, 0, true);
            initBallList(balls_thevenLine, 10, 0, true);
            arr_infos.add(balls_firstLine);
            arr_infos.add(balls_twoLine);
            arr_infos.add(balls_threeLine);
            arr_infos.add(balls_fourLine);
            arr_infos.add(balls_fiveLine);
            arr_infos.add(balls_sixLine);
            arr_infos.add(balls_thevenLine);
        }

        ballView_firstLine.setBallInfoList(balls_firstLine);
        ballView_twoline.setBallInfoList(balls_twoLine);
        ballView_threeline.setBallInfoList(balls_threeLine);
        ballView_fourline.setBallInfoList(balls_fourLine);
        ballView_fiveline.setBallInfoList(balls_fiveLine);
        ballView_sixline.setBallInfoList(balls_sixLine);
        ballView_thevenline.setBallInfoList(balls_thevenLine);
    }

    @Override
    public void ballClick(WeiZhi info) {
        // TODO Auto-generated method stub
        WeiZhi w = info;
        // TODO Auto-generated method stub
        w.setType(0);
        int left = w.getLeft() - 5;
        int right = w.getRight() + 5;
        int top = (int) ((ball_linear.getY1() - w.getPointY() + w.getTop())
                - (w.getBottom() - w.getTop()) - 10);
        int bottom = (int) (ball_linear.getY1() - w.getPointY() + w.getTop());
        Rect rect = new Rect(left, top, right, bottom);
        Log.e("FramLayout   画球区", ".............  gettype:" + w.getType());
        moveQiu.setRect(rect, w.getNum(), w.getType(), w.isShowMoveBall);
    }


    @Override
    public void ballClick() {
        // TODO Auto-generated method stub

        int zhushu = getZhushu();
        int money = zhushu * 2;
        bottom_zhushu.setText(zhushu + "注 ");
        bottom_money.setText("共" + money + "米");

    }


    private int getZhushu() {
        int zhushu = 1;
        for (int i = 0; i < arr_infos.size(); i++) {
            int j = 0;
            List<BallInfo> infos = arr_infos.get(i);
            for (BallInfo info : infos) {
                if (info.isSelected()) {
                    j++;
                }
            }
            zhushu *= j;
        }
        return zhushu;
    }

    @Override
    public boolean touZhu() {
        // TODO Auto-generated method stub
        int zhuShu = getZhushu();
        if (zhuShu > 0) {
            int lotteryId = zhuShu > 1 ? LotteryIDUtils.QXC_LOTTERY_COMMON_FU_ID_INT
                    : LotteryIDUtils.QXC_LOTTERY_COMMON_DAN_ID_INT;
            touZhu1(lotteryId, zhuShu);
            return true;
        } else {
            return false;
        }
    }

    @Override
    public int getLotteryId() {
        // TODO Auto-generated method stub
        return LotteryIDUtils.QXC_LOTTERY_ID_INT;
    }

    @Override
    public void randomBallNum() {
        // TODO Auto-generated method stub
        int first = RandomFactory.getRandom(LotteryIDUtils.QXC_LOTTERY_ID_INT).randomDanHang(1)[0];
        int two = RandomFactory.getRandom(LotteryIDUtils.QXC_LOTTERY_ID_INT).randomDanHang(1)[0];
        int thire = RandomFactory.getRandom(LotteryIDUtils.QXC_LOTTERY_ID_INT).randomDanHang(1)[0];
        int four = RandomFactory.getRandom(LotteryIDUtils.QXC_LOTTERY_ID_INT).randomDanHang(1)[0];
        int five = RandomFactory.getRandom(LotteryIDUtils.QXC_LOTTERY_ID_INT).randomDanHang(1)[0];
        int six = RandomFactory.getRandom(LotteryIDUtils.QXC_LOTTERY_ID_INT).randomDanHang(1)[0];
        int seven = RandomFactory.getRandom(LotteryIDUtils.QXC_LOTTERY_ID_INT).randomDanHang(1)[0];

        initBallList(balls_firstLine, 10, 0, true);
        initBallList(balls_twoLine, 10, 0, true);
        initBallList(balls_threeLine, 10, 0, true);
        initBallList(balls_fourLine, 10, 0, true);
        initBallList(balls_fiveLine, 10, 0, true);
        initBallList(balls_sixLine, 10, 0, true);
        initBallList(balls_thevenLine, 10, 0, true);

        balls_firstLine.get(first).setSelected(true);
        balls_twoLine.get(two).setSelected(true);
        balls_threeLine.get(thire).setSelected(true);
        balls_fourLine.get(four).setSelected(true);
        balls_fiveLine.get(five).setSelected(true);
        balls_sixLine.get(six).setSelected(true);
        balls_thevenLine.get(seven).setSelected(true);

        ballView_firstLine.invalidate();
        ballView_twoline.invalidate();
        ballView_threeline.invalidate();
        ballView_fourline.invalidate();
        ballView_fiveline.invalidate();
        ballView_sixline.invalidate();
        ballView_thevenline.invalidate();

        ballClick();
    }

    @Override
    protected void addHistoryView(ArrayList<PrizeHistoryItem> historyItems, boolean isWaitting) {
        ll_history.removeAllViews();
        ll_history.addView(new NumHistoryPrizeView(getContext(), historyItems).getView());
    }

    @Override
    public void clear() {
        // TODO Auto-generated method stub
        bottom_zhushu.setText(0 + "注 ");
        bottom_money.setText("共" + 0 + "米");

        initBallList(balls_firstLine, 10, 0, true);
        initBallList(balls_twoLine, 10, 0, true);
        initBallList(balls_threeLine, 10, 0, true);
        initBallList(balls_fourLine, 10, 0, true);
        initBallList(balls_fiveLine, 10, 0, true);
        initBallList(balls_sixLine, 10, 0, true);
        initBallList(balls_thevenLine, 10, 0, true);

        ballView_firstLine.invalidate();
        ballView_twoline.invalidate();
        ballView_threeline.invalidate();
        ballView_fourline.invalidate();
        ballView_fiveline.invalidate();
        ballView_sixline.invalidate();
        ballView_thevenline.invalidate();
    }

    @Override
    public String getHelpUrl() {
        // TODO Auto-generated method stub
        return HelpUrlConstants.Help_QXC;
    }

    @Override
    public String getHelpTitle() {
        // TODO Auto-generated method stub
        return getString(R.string.help_qxc);
    }

    @Override
    public void onRightClick() {
        // TODO Auto-generated method stub
        super.onRightClick();
        toHelp();
    }

    @Override
    protected void popupClickItem(int arg2) {
        // TODO Auto-generated method stub

    }

    @Override
    protected void addMissValue() {
        super.addMissValue();
        try {
            if (missInfo != null) {
                ballView_firstLine.setMissValues(missInfo.missValue("2004MV_ZX").get(0));
                ballView_twoline.setMissValues(missInfo.missValue("2004MV_ZX").get(1));
                ballView_threeline.setMissValues(missInfo.missValue("2004MV_ZX").get(2));
                ballView_fourline.setMissValues(missInfo.missValue("2004MV_ZX").get(3));
                ballView_fiveline.setMissValues(missInfo.missValue("2004MV_ZX").get(4));
                ballView_sixline.setMissValues(missInfo.missValue("2004MV_ZX").get(5));
                ballView_thevenline.setMissValues(missInfo.missValue("2004MV_ZX").get(6));

            }
        } catch (Exception e) {

        }


    }

    @Override
    public Intent getTrendIntent() {
        Intent intent = new Intent(getContext(), QXCTrendActivity.class);
        intent.putExtra(IntentParamUtils.LOTTERYID, getLotteryId());
        return intent;
    }
}
